package com.qianchang.microfinance.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.qianchang.microfinance.bean.AreaJsonBean;
import com.qianchang.microfinance.global.DMApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: <读取Json文件的工具类>
 * 修 改 人: sushuo
 * 修改时间: 2017-3-21
 */

public class GetJsonDataUtil {

    private static String areaStr = null;
    private static ArrayList<AreaJsonBean> provinceList;

    private static GetJsonDataUtil instance = null;

    private GetJsonDataUtil() {
        areaStr = getJson(DMApplication.getInstance(), "city.json");
        parseData();
    }

    public static GetJsonDataUtil newInstance() {
        if (null == instance) {
            synchronized (GetJsonDataUtil.class) {
                if (null == instance) {
                    instance = new GetJsonDataUtil();
                }
            }
        }
        return instance;
    }

    public String getJson(Context context, String fileName) {
        if (null == areaStr) {
            StringBuilder stringBuilder = new StringBuilder();
            try {
                AssetManager assetManager = context.getAssets();
                BufferedReader bf = new BufferedReader(new InputStreamReader(
                        assetManager.open(fileName)));
                String line;
                while ((line = bf.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            areaStr = stringBuilder.toString();
        }
        return areaStr;
    }

    public ArrayList<AreaJsonBean> parseData() {
        if (null == areaStr) {
            areaStr = getJson(DMApplication.getInstance(), "city.json");
            if (null == areaStr) {
                return provinceList;
            }
        }
        if (null == provinceList) {
            try {
                Gson gson = new Gson();
                provinceList = gson.fromJson(areaStr, new TypeToken<ArrayList<AreaJsonBean>>() {
                }.getType());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return provinceList;
    }


    public String getProvinceById(String provinceId) {
        if (null == provinceList) {
            provinceList = parseData();
        }
        for (AreaJsonBean areaJsonBean : provinceList) {
            String id = areaJsonBean.getId();
            if (provinceId != null && provinceId.equals(id)) {
                return areaJsonBean.getName();
            }
        }
        return "";
    }


    public String getCityById(String cityId) {
        if (null == provinceList) {
            provinceList = parseData();
        }
        for (AreaJsonBean areaJsonBean : provinceList) {
            List<AreaJsonBean.CityBean> cityBeanList = areaJsonBean.getChildren();
            for (AreaJsonBean.CityBean cityBean : cityBeanList) {
                String id = cityBean.getId();
                if (cityId != null && cityId.equals(id)) {
                    return cityBean.getName();
                }
            }
        }
        return "";
    }

    public String getPrinceAndCityById(String provinceId, String cityId) {
        StringBuilder result = new StringBuilder();
        if (null == provinceList) {
            provinceList = parseData();
        }
        for (AreaJsonBean areaJsonBean : provinceList) {
            if (null != provinceId && provinceId.equals(areaJsonBean.getId())) {
                String provinceName = areaJsonBean.getName();
                result.append(provinceName);
                List<AreaJsonBean.CityBean> cityBeanList = areaJsonBean.getChildren();
                for (AreaJsonBean.CityBean cityBean : cityBeanList) {
                    String id = cityBean.getId();
                    String name = cityBean.getName();
                    if (cityId != null && cityId.equals(id) && !provinceName.equals(name)) {
                        result.append(name);
                        break;
                    }
                }
                break;
            }
        }
        return result.toString();
    }

    public String getAreaById(String areaId) {
        if (null == provinceList) {
            provinceList = parseData();
        }
        for (AreaJsonBean areaJsonBean : provinceList) {
            List<AreaJsonBean.CityBean> cityBeanList = areaJsonBean.getChildren();
            for (AreaJsonBean.CityBean cityBean : cityBeanList) {
                List<AreaJsonBean.AreaBean> children = cityBean.getChildren();
                for (AreaJsonBean.AreaBean child : children) {
                    String id = child.getId();
                    if (areaId != null && areaId.equals(id)) {
                        return child.getName();
                    }
                }
            }
        }
        return "";
    }

}

